/**
 *  Class ConsoleApplicationProcessWrapper is part of the project OtrMediaManager
    Copyright (C) 2014  Gerrit Eichel

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package otrmediamanager.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * This class implements a little process wrapper to call console applications.
 */
public class ConsoleApplicationProcessWrapper extends Thread {
	
	/** The command line argument list.*/
	private List<String> _processCommand = null;
	/** The last read console output line. */
	private String _consoleLine = "";

	/**
	 * Initializes a new ProcessWrapper instance.
	 * @param processCommand The command line argument list.
	 */
	public ConsoleApplicationProcessWrapper(List<String> processCommand) {
		this.setProcessCommand(processCommand);
	}

	/**
	 * Return process command line list.
	 * @return The command line list.
	 */
	public List<String> getProcessCommand() {
		return this._processCommand;
	}
	/**
	 * Sets process command line list.
	 * @param processCommand The command line list to be set.
	 */
	private void setProcessCommand(List<String> processCommand) {
		if (processCommand == null) {
			throw new java.lang.IllegalArgumentException("Property <ProcessCommand> is not initialized!");
		}
		if (processCommand.isEmpty()) {
			throw new java.lang.IllegalArgumentException("Property <ProcessCommand> is empty!");
		}
		this._processCommand = processCommand;
	}
	
	/**
	 * Returns last read console output line.
	 * @return The console output line.
	 */
	public synchronized String getLastConsoleLine() {
		return this._consoleLine;
	}

	/**
	 * Starts process to wrapped and reads process console output. 
	 */
	@Override
	public void run() {
		// Configure our process.
		ProcessBuilder builder = new ProcessBuilder();
		builder.command(this.getProcessCommand());
		builder.redirectErrorStream(true);
		// Start external process.
		Process myProcess = null;
        try {
			myProcess = builder.start();
	        // Get process console output stream.
	        InputStream is = myProcess.getInputStream();
	        InputStreamReader isr = new InputStreamReader(is);	
	        BufferedReader br = new BufferedReader(isr);
	        // Read console output stream.	
			while ((this._consoleLine = br.readLine()) != null) {
				super.sleep(1);
			}
	        // Close everything.
	    	br.close();
	    	isr.close();
	    	is.close();
        } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
}